<?php


namespace api\crm\controller;


use app\admin\model\AuthAppidModel;
use cmf\controller\RestBaseController;

class OpenPlatFormController extends RestBaseController
{
    public function index()
    {

        $res = hook('open_plat_form_ticket');
        $this->success('1111', $res);
    }

    public function authcode()
    {
        $auth_code = input('auth_code');
        $company_id = input('company_id');
        $param['auth_code'] = $auth_code;
        $res = hook('open_plat_form_handle_authorize', $param);
        $model = AuthAppidModel::create([
            'appid' => $res[0]['authorization_info']['authorizer_appid'],
            'company_id' => $company_id,
            'access_token' => $res[0]['authorization_info']['authorizer_access_token'],
            'expires_in' => $res[0]['authorization_info']['expires_in'],
            'refresh_token' => $res[0]['authorization_info']['authorizer_refresh_token']
        ]);
        $model->save();
        return '绑定成功';
        $this->success('22', $res);
    }

    public function get_open_plat_form_url()
    {
        $company_id = input('company_id');
        $setting = AuthAppidModel::where('company_id', $company_id)->find();
        if ($setting) {
            $this->success('已绑定');
        } else {
            $param['company_id'] = $company_id;
            $res = hook('get_open_plat_form_url', $param);
            $this->success('', $res);
        }

    }
}